package pl.szpadel.android.gadu;

/// Holds and interprets GG status and status description
public class Status {

	/// Raw GG status value
	private int mStatus;
	
	/// status description
	private String mDescription;
	
	/// Initializes from description and GG status value
	public Status(int s, String desc) {
		mStatus = s;
		mDescription = desc;
	}
	
	/// Initializes from description and config string
	public Status(String s, String desc) {
		if (s == "fcc") mStatus = GG_STATUS_FFC;
		else if (s.equals("avail")) mStatus = GG_STATUS_AVAIL;
		else if (s.equals("busy")) mStatus = GG_STATUS_BUSY;
		else if (s.equals("invisible")) mStatus = GG_STATUS_INVISIBLE;
		else if (s.equals("dns")) mStatus = GG_STATUS_DND;
		else mStatus = GG_STATUS_AVAIL; // default
		
		mDescription = desc;
	}
	
	/// Initializes with empty values
	public Status() {
		mDescription = "";
		mStatus = -1;
	}
	
	/// Returns description
	public String getDescription() {
		return mDescription;
	}
	
	/// Convenience - is online (that is not offline and not invisible and not unknown)
	public boolean isOnline() {
		if (mStatus == -1) return false;
		if (getGGStatusNoDesc() == GG_STATUS_NOT_AVAIL) return false;
		if (getGGStatusNoDesc() == GG_STATUS_INVISIBLE) return false;
		
		return true;
	}
	
	/// Returns icon resource corresponding with status
	public int getIcon() {
		switch(mStatus) {
		case GG_STATUS_FFC:
		case GG_STATUS_FFC_DESCR:
		case GG_STATUS_AVAIL:
		case GG_STATUS_AVAIL_DESCR:
			return android.R.drawable.presence_online;
		case GG_STATUS_BUSY:
		case GG_STATUS_BUSY_DESCR:
			return android.R.drawable.presence_away; 
		case GG_STATUS_DND:
		case GG_STATUS_DND_DESCR:
			return android.R.drawable.presence_busy;
		default:
			return android.R.drawable.presence_offline;
		}
	}
	
	/// Returns GG status in non _descr version (used when no status description is set)
	public int getGGStatusNoDesc() {
		switch(mStatus) {
		case GG_STATUS_FFC_DESCR:
				return GG_STATUS_FFC;
		case GG_STATUS_AVAIL_DESCR:
			return GG_STATUS_AVAIL;
		case GG_STATUS_BUSY_DESCR:
			return GG_STATUS_BUSY;
		case GG_STATUS_DND_DESCR:
			return GG_STATUS_DND;
		case GG_STATUS_INVISIBLE_DESCR:
			return GG_STATUS_INVISIBLE;
		case GG_STATUS_NOT_AVAIL_DESCR:
			return GG_STATUS_NOT_AVAIL;

		default:
			return mStatus;
		}
	}
	
	/// Returns GG status in _descr version (used when status description is set)
	public int getGGStatusDesc() {
		switch(mStatus) {
		case GG_STATUS_FFC:
				return GG_STATUS_FFC_DESCR;
		case GG_STATUS_AVAIL:
			return GG_STATUS_AVAIL_DESCR;
		case GG_STATUS_BUSY:
			return GG_STATUS_BUSY_DESCR;
		case GG_STATUS_DND:
			return GG_STATUS_DND_DESCR;
		case GG_STATUS_INVISIBLE:
			return GG_STATUS_INVISIBLE_DESCR;
		case GG_STATUS_NOT_AVAIL:
			return GG_STATUS_NOT_AVAIL_DESCR;

		default:
			return mStatus;
		}
	}
	
	/// Returns GG status (modified for existence (or not) of the description
	public int getGGStatus() {
		if (mDescription.length() > 0){
			return getGGStatusDesc();
		} else {
			return getGGStatusNoDesc();
		}
	}
	
	/// Returns human-readable status description used in config and debugging
	public String toConfigString() {
		switch(getGGStatusNoDesc()){
			case GG_STATUS_FFC:
				return "ffc";
			case GG_STATUS_AVAIL:
				return "avail";
			case GG_STATUS_BUSY:
				return "busy";
			case GG_STATUS_DND:
				return "dnd";
			case GG_STATUS_INVISIBLE:
			case GG_STATUS_NOT_AVAIL: // connection status is controlled separately
				return "invisible";
		
			default:
				return "unknown";
		}
	}
	
	/// Serialization
	public String toString() {
		return "Status [ " + toConfigString() + "(0x" + Integer.toHexString(mStatus) + "), " + mDescription + " ]";
	}
	
	// GG values
	public static final int GG_STATUS_NOT_AVAIL	= 0x0001;	
	public static final int GG_STATUS_NOT_AVAIL_DESCR	= 0x0015;	
	public static final int GG_STATUS_FFC	= 0x0017;
	public static final int GG_STATUS_FFC_DESCR	= 0x0018;	
	public static final int GG_STATUS_AVAIL	= 0x0002;	
	public static final int GG_STATUS_AVAIL_DESCR	= 0x0004;	
	public static final int GG_STATUS_BUSY	= 0x0003;	
	public static final int GG_STATUS_BUSY_DESCR	= 0x0005;	
	public static final int GG_STATUS_DND	= 0x0021;	
	public static final int GG_STATUS_DND_DESCR	= 0x0022;	
	public static final int GG_STATUS_INVISIBLE	= 0x0014;	
	public static final int GG_STATUS_INVISIBLE_DESCR = 0x0016;
	public static final int GG_STATUS_BLOCKED	= 0x0006;	
	public static final int GG_STATUS_IMAGE_MASK	= 0x0100;
	public static final int GG_STATUS_ADAPT_STATUS_MASK	= 0x0400;	
	public static final int GG_STATUS_DESCR_MASK	= 0x4000;	
	public static final int GG_STATUS_FRIENDS_MASK	= 0x8000;	
}
